package org.abc.fund.controller.funds;

import org.abc.fund.dto.FundManagerDTO;
import org.abc.fund.entity.funds.FundManager;
import org.abc.fund.service.funds.FundManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/fund-manager")
public class FundManagerController {

    @Autowired
    private FundManagerService fundManagerService;

    // 🔍 获取所有基金经理（可选模糊搜索）
    @GetMapping("/all")
    public List<FundManager> getAllManagers(@RequestParam(required = false) String name) {
        return fundManagerService.getAllManagers(name);
    }

    // ✅ 分页获取基金经理 DTO（可选关键词）
    @GetMapping("/page")
    public Page<FundManagerDTO> getPagedManagers(
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size
    ) {
        return fundManagerService.getManagersDTOByPage(page, size, keyword);
    }

    // 🔍 根据标签筛选出包含所选标签的基金经理（返回扁平化 DTO）
    @GetMapping("/by-tags")
    public List<FundManagerDTO> getManagersByTags(@RequestParam List<String> tags) {
        return fundManagerService.getManagersByTags(tags);
    }

    // 📋 获取经理详情（包含基金列表）
    @GetMapping("/detail/{id}")
    public FundManagerDTO getManagerDetail(@PathVariable Long id) {
        return fundManagerService.getManagerDetail(id);
    }

    // 📋 获取某个经理管理的基金（支持展开功能）
    @GetMapping("/funds")
    public List<?> getFundsByManagerName(@RequestParam String managerName) {
        return fundManagerService.getFundsByManagerName(managerName);
    }

    // ➕ 添加基金经理
    @PostMapping("/add")
    public FundManager addManager(@RequestBody FundManager manager) {
        return fundManagerService.addManager(manager);
    }

    // ✏️ 修改基金经理
    @PutMapping("/update/{id}")
    public FundManager updateManager(@PathVariable Long id, @RequestBody FundManager updated) {
        return fundManagerService.updateManager(id, updated);
    }

    // ❌ 删除基金经理
    @DeleteMapping("/delete/{id}")
    public void deleteManager(@PathVariable Long id) {
        fundManagerService.deleteManager(id);
    }

    // 🔍 获取单个基金经理实体
    @GetMapping("/{id}")
    public FundManager getManager(@PathVariable Long id) {
        return fundManagerService.getManagerById(id);
    }
}
